/**
 * 将图片url转化为base64
 */
export function fetchImageAsBase64() {
  const cacheData = {}
  return async function (url) {
    if (cacheData[url]) {
      return cacheData[url]
    } else {
      return new Promise((resolve, reject) => {
        const img = new Image()
        img.crossOrigin = 'Anonymous' // 注意：如果图片在CORS策略下，需要设置这个属性
        img.onerror = reject
        img.src = url
        img.onload = () => {
          const canvas = document.createElement('canvas')
          const ctx = canvas.getContext('2d')
          canvas.width = img.width
          canvas.height = img.height
          ctx.drawImage(img, 0, 0, img.width, img.height)

          // 将canvas内容转换为Base64
          const base64DataUrl = canvas.toDataURL('image/png') // 可以指定其他格式，如'image/jpeg'
          cacheData[url] = base64DataUrl
          resolve(base64DataUrl)
        }
      })
    }
  }
}
